//
//  UIButton+QYBlock.m
//  CarPooling
//
//  Created by Dxc_iOS on 2018/6/6.
//  Copyright © 2018年 代星创. All rights reserved.
//

#import "UIButton+QYBlock.h"
#import <objc/runtime.h>
//定义Block
typedef void(^QY_ButtonEventsBlock)(void);

@interface UIButton ()
/** 事件回调的block */
@property (nonatomic, copy) QY_ButtonEventsBlock qy_buttonEventsBlock;

@end
@implementation UIButton (QYBlock)
//------- 添加属性 -------//

static void *qy_buttonEventsBlockKey = &qy_buttonEventsBlockKey;

- (QY_ButtonEventsBlock)cq_buttonEventsBlock {
    return objc_getAssociatedObject(self, &qy_buttonEventsBlockKey);
}

- (void)setQy_buttonEventsBlock:(QY_ButtonEventsBlock)qy_buttonEventsBlock {
    objc_setAssociatedObject(self, &qy_buttonEventsBlockKey, qy_buttonEventsBlock, OBJC_ASSOCIATION_COPY);
}

/**
 给按钮绑定事件回调block
 
 @param block 回调的block
 @param controlEvents 回调block的事件
 */
- (void)qy_addButtonEventHandler:(void (^)(void))block forControlEvents:(UIControlEvents)controlEvents {
    self.qy_buttonEventsBlock = block;
    [self addTarget:self action:@selector(qy_blcokButtonClicked) forControlEvents:controlEvents];
}

// 按钮点击
- (void)qy_blcokButtonClicked {
    !self.qy_buttonEventsBlock ?: self.qy_buttonEventsBlock();
}
@end
